package com.coldworks.coldjoke.ui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.coldjoke.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class JokeActionBar extends RelativeLayout {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private LinearLayout linearLayout0;
	private LinearLayout linearLayout1;
	private Button jokeAll;
	private Button jokeHot;
	private Button jokeShit;

	private LinearLayout likeBtn;
	private LinearLayout dislikeBtn;
	private LinearLayout shareBtn;
	private LinearLayout commentBtn;

	private ToggleButton focusBtn;

	private Context context;
	private int textColor;
	private int textColorCold;

	private JokeActionBarListener jabListener;

	private int showingListIndex = -1;

	public JokeActionBar(Context context) {
		super(context);
		setUpViews(context);
	}

	public JokeActionBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		setUpViews(context);
	}

	public JokeActionBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setUpViews(context);
	}

	private void setUpViews(Context context) {
		this.context = context;
		// RelativeLayout operationBar = (RelativeLayout) LayoutInflater.from(
		// context).inflate(R.layout.operation_bar_joke, null);
		// //
		// linearLayout0 = (LinearLayout) operationBar
		// .findViewById(R.id.button_all);
		// linearLayout1 = (LinearLayout) operationBar
		// .findViewById(R.id.linearlayout1);
		// jokeAll = (Button) linearLayout0.getChildAt(0);
		// jokeHot = (Button) operationBar.findViewById(R.id.button_hot);
		// jokeShit = (Button) operationBar.findViewById(R.id.button_shit);
		//
		// likeBtn = (LinearLayout) operationBar.findViewById(R.id.button_like);
		// dislikeBtn = (LinearLayout) operationBar
		// .findViewById(R.id.button_dislike);
		// focusBtn = (ToggleButton)
		// operationBar.findViewById(R.id.button_focus);
		// shareBtn = (LinearLayout)
		// operationBar.findViewById(R.id.button_share);
		// commentBtn = (LinearLayout) operationBar
		// .findViewById(R.id.button_comment);
		// textColor = context.getResources().getColor(R.color.text_color_2);
		// textColorCold = context.getResources().getColor(R.color.text_cold);
		// addView(operationBar);

		linearLayout0.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (setShowingListIndex(0)) {
					jabListener.jabShowListAll();
				}
			}
		});

		jokeHot.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (setShowingListIndex(1)) {
					jabListener.jabShowListHot();
				}
			}
		});

		jokeShit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (setShowingListIndex(2)) {
					jabListener.jabShowListShit();
				}
			}
		});

		likeBtn.getChildAt(0).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (userState != null) {
					userState.usLike();
				}
			}
		});

		dislikeBtn.getChildAt(0).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (userState != null) {
					userState.usDislike();
				}
			}
		});

		shareBtn.getChildAt(0).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (jabListener != null) {
					jabListener.jabShare();
				}
			}
		});

		commentBtn.getChildAt(0).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (userState != null) {
					userState.usComment();
				}
			}
		});

		focusBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					final boolean isChecked) {
				if (isChecked) {
					setBarStateList();
				} else {
					setBarStateJoke();
				}
			}
		});
		setShowingListIndex(0);
	}

	public void setJokeActionBarListener(JokeActionBarListener pJabListener) {
		jabListener = pJabListener;
	}

	@SuppressWarnings("deprecation")
	public boolean setShowingListIndex(int index) {
		if (showingListIndex == index) {
			return false;
		}
		showingListIndex = index;
		switch (index) {
		case 0:
			jokeAll.setTextColor(textColorCold);
			jokeHot.setTextColor(textColor);
			jokeShit.setTextColor(textColor);
			jokeAll.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.shape_underline_blue));
			jokeHot.setBackgroundDrawable(null);
			jokeShit.setBackgroundDrawable(null);
			break;
		case 1:
			jokeHot.setTextColor(textColorCold);
			jokeAll.setTextColor(textColor);
			jokeShit.setTextColor(textColor);
			jokeHot.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.shape_underline_blue));
			jokeAll.setBackgroundDrawable(null);
			jokeShit.setBackgroundDrawable(null);
			break;
		case 2:
			jokeShit.setTextColor(textColorCold);
			jokeHot.setTextColor(textColor);
			jokeAll.setTextColor(textColor);
			jokeShit.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.shape_underline_blue));
			jokeHot.setBackgroundDrawable(null);
			jokeAll.setBackgroundDrawable(null);
			break;

		default:
			break;
		}
		return true;
	}

	public interface JokeActionBarListener {

		public void jabLike();

		public void jabDislike();

		public void jabShare();

		public void jabComment();

		public void jabShowListAll();

		public void jabShowListHot();

		public void jabShowListShit();

		public void jabLogin();

		public void jabShowJoke();

	}

	private LikeStateBase likeState;
	public LikeStateLoading likeStateLoading = new LikeStateLoading();
	public LikeStateEnable likeStateEnable = new LikeStateEnable();
	public LikeStateDisable likeStateDisable = new LikeStateDisable();

	// 状态机
	public void setLikeState(LikeStateBase newLikeState, Context ctx,
			boolean liked, boolean disliked) {
		if (likeState != newLikeState) {
			likeState = newLikeState;
			if (likeState != null) {
				likeState.lsStart(ctx, liked, disliked);
			}
		}
	}

	public interface LikeStateBase {
		public void lsStart(Context ctx, boolean liked, boolean disliked);

		public void lsLike();

		public void lsDislike();
	}

	public class LikeStateLoading implements LikeStateBase {
		public void lsStart(Context ctx, boolean liked, boolean disliked) {
			likeBtn.getChildAt(0).setEnabled(true);
			dislikeBtn.getChildAt(0).setEnabled(true);
			likeBtn.getChildAt(0).setBackgroundResource(
					R.drawable.selector_action_btn_like);
			dislikeBtn.getChildAt(0).setBackgroundResource(
					R.drawable.selector_action_btn_dislike);
		}

		public void lsLike() {
			Toast.makeText(context, "正在获取数据，请稍候", Toast.LENGTH_SHORT).show();
		}

		public void lsDislike() {
			Toast.makeText(context, "正在获取数据，请稍候", Toast.LENGTH_SHORT).show();
		}
	}

	public class LikeStateEnable implements LikeStateBase {
		public void lsStart(Context ctx, boolean liked, boolean disliked) {
			likeBtn.getChildAt(0).setEnabled(true);
			dislikeBtn.getChildAt(0).setEnabled(true);
			likeBtn.getChildAt(0).setBackgroundResource(
					R.drawable.selector_action_btn_like);
			dislikeBtn.getChildAt(0).setBackgroundResource(
					R.drawable.selector_action_btn_dislike);
		}

		public void lsLike() {
			jabListener.jabLike();
		}

		public void lsDislike() {
			jabListener.jabDislike();
		}
	}

	public class LikeStateDisable implements LikeStateBase {
		public void lsStart(Context ctx, boolean liked, boolean disliked) {
			likeBtn.getChildAt(0).setEnabled(false);
			dislikeBtn.getChildAt(0).setEnabled(false);
			if (liked) {
				likeBtn.getChildAt(0).setBackgroundResource(
						R.drawable.ic_action_like_pressed);
			} else {
				likeBtn.getChildAt(0).setBackgroundResource(
						R.drawable.ic_action_like_disable);
			}

			if (disliked) {
				dislikeBtn.getChildAt(0).setBackgroundResource(
						R.drawable.ic_action_dislike_pressed);
			} else {
				dislikeBtn.getChildAt(0).setBackgroundResource(
						R.drawable.ic_action_dislike_disable);
			}
		}

		public void lsLike() {
			// Toast.makeText(context, "已经like过", Toast.LENGTH_SHORT).show();
		}

		public void lsDislike() {
			// Toast.makeText(context, "已经dislike过", Toast.LENGTH_SHORT).show();
		}
	}

	private UserStateBase userState;
	public UserStateLogout userStateLogout = new UserStateLogout();
	public UserStateLogin userStateLogin = new UserStateLogin();

	// 状态机
	public void setUserState(UserStateBase newUserState) {
		if (userState != newUserState) {
			userState = newUserState;
		}
	}

	public interface UserStateBase {

		public void usLike();

		public void usDislike();

		public void usComment();
	}

	public class UserStateLogout implements UserStateBase {

		public void usLike() {
			jabListener.jabLogin();
		}

		public void usDislike() {
			jabListener.jabLogin();
		}

		public void usComment() {
			jabListener.jabLogin();
		}
	}

	public class UserStateLogin implements UserStateBase {
		public void usLike() {
			likeState.lsLike();
		}

		public void usDislike() {
			likeState.lsDislike();
		}

		public void usComment() {
			jabListener.jabComment();
		}
	}

	private BarStateJoke barStateJoke = new BarStateJoke();
	private BarStateHidden barStateHidden = new BarStateHidden();
	private BarStateListOne barStateListOne = new BarStateListOne();
	private BarStateListThree barStateListThree = new BarStateListThree();
	private BarStateBase barState;
	private BarStateBase barStateList;

	// 状态机
	public void setBarStateJoke() {
		barState = barStateJoke;
		if (barState != null) {
			barState.bsStart();
		}
	}

	public void setBarStateList() {
		barState = barStateList;
		if (barState != null) {
			barState.bsStart();
		}
	}

	public void setBarStateHidden() {
		barState = barStateHidden;
		if (barState != null) {
			barState.bsStart();
		}
	}

	public void startBarStateInit() {
		barStateList = barStateListOne;
	}

	public void stopBarStateInit() {
		barStateList = barStateListThree;
	}

	public interface BarStateBase {

		public void bsStart();

	}

	public class BarStateJoke implements BarStateBase {

		public void bsStart() {
			JokeActionBar.this.setVisibility(View.VISIBLE);
			linearLayout0.setVisibility(View.GONE);
			linearLayout1.setVisibility(View.GONE);
			likeBtn.setVisibility(View.VISIBLE);
			dislikeBtn.setVisibility(View.VISIBLE);
			shareBtn.setVisibility(View.VISIBLE);
			commentBtn.setVisibility(View.VISIBLE);
			if (focusBtn.isChecked()) {
				focusBtn.setChecked(false);
				return;
			}
			if (jabListener != null) {
				jabListener.jabShowJoke();
			}
		}
	}

	public class BarStateHidden implements BarStateBase {

		public void bsStart() {
			JokeActionBar.this.setVisibility(View.INVISIBLE);
		}
	}

	public class BarStateListOne implements BarStateBase {
		public void bsStart() {
			JokeActionBar.this.setVisibility(View.VISIBLE);
			linearLayout0.setVisibility(View.VISIBLE);
			linearLayout1.setVisibility(View.INVISIBLE);
			likeBtn.setVisibility(View.GONE);
			dislikeBtn.setVisibility(View.GONE);
			shareBtn.setVisibility(View.GONE);
			commentBtn.setVisibility(View.GONE);
			if (!focusBtn.isChecked()) {
				focusBtn.setChecked(true);
				return;
			}
			setShowingListIndex(0);
			jabListener.jabShowListAll();
		}
	}

	public class BarStateListThree implements BarStateBase {
		public void bsStart() {
			JokeActionBar.this.setVisibility(View.VISIBLE);
			linearLayout0.setVisibility(View.VISIBLE);
			linearLayout1.setVisibility(View.VISIBLE);
			likeBtn.setVisibility(View.GONE);
			dislikeBtn.setVisibility(View.GONE);
			shareBtn.setVisibility(View.GONE);
			commentBtn.setVisibility(View.GONE);
			if (!focusBtn.isChecked()) {
				focusBtn.setChecked(true);
				return;
			}
			switch (showingListIndex) {
			case 0:
				jabListener.jabShowListAll();
				break;
			case 1:
				jabListener.jabShowListHot();
				break;
			case 2:
				jabListener.jabShowListShit();
				break;
			}
		}
	}

}
